﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedHouse.Data.Entities;
using RedHouse.Data.Service;
using RedHouse.Data.Base;
using RedHouse.Data;
using RedHouse.Web.common;
using System.Text;

namespace RedHouse.Web
{
    public partial class Dish : PageBase
    {
        public List<RedHouseDish> Dishes { get; set; }
        public RedHouseDish DishDetail { get; set; }
        private IDishService _dishService;
        private ICategoryService _categoryService;
        private ILanguageService _languageService;
        public List<RedHouse.Data.Image> ImageList { get; set; }
        public string  PageTitle { get; set; }
        public string Category { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientScriptManager cs = Page.ClientScript;
            String csname = "PopupScript";
            Type cstype = this.GetType();

            Category = Request.QueryString["category"] == null ? string.Empty : Request.QueryString["category"].ToString();
            if (Category == "Specialties")
            {
                PageTitle = RedHouse.Web.Resources.lblSpecialties;
                if (!cs.IsStartupScriptRegistered(cstype, csname))
                {
                    StringBuilder script = new StringBuilder();
                    script.Append("<script type=text/javascript>");
                    script.Append("$(document).ready(function() { $('#nav li').removeClass('active');$('#menu-specialties').addClass('active'); });");
                    script.Append("</script>");

                    cs.RegisterStartupScript(cstype, csname, script.ToString());
                }
            }
            else if (Category == "Seafood")
            {
                PageTitle = RedHouse.Web.Resources.lblSeafood;
                if (!cs.IsStartupScriptRegistered(cstype, csname))
                {
                    StringBuilder script = new StringBuilder();
                    script.Append("<script type=text/javascript>");
                    script.Append("$(document).ready(function() { $('#nav li').removeClass('active');$('#menu-seafood').addClass('active'); });");
                    script.Append("</script>");

                    cs.RegisterStartupScript(cstype, csname, script.ToString());
                }
            }
            //PageTitle = string.Format("Our {0}", Category);
            string id = Request.QueryString["id"] == null ? string.Empty : Request.QueryString["id"].ToString();
            Initialize(Category, id);         
        }

        private void Initialize(string category, string id)
        {
            _dishService = new DishService();
            _dishService = new DishService();
            _categoryService = new CategoryService();
            _languageService = new LanguageService();

            RedHouseCategory dishCategory = _categoryService.GetCategoryByName(category);
            Dishes = _dishService.GetAllDishByCategory(dishCategory.Id);
            if (Session["location"] != null)
            {
                int location = int.Parse(Session["location"].ToString());
                Dishes = Dishes.Where(d => d.LocationID == location || d.LocationID == 3).ToList();
            }

            Language language;
            if (Session["Culture"] != null && Session["Culture"].ToString() == "en-US")
            {
                language = _languageService.GetAllLanguage().Where(l => l.Description == "English").FirstOrDefault();
            }
            else
            {
                language = _languageService.GetAllLanguage().Where(l => l.Description != "English").FirstOrDefault();
            }

            if (language != null)
            {
                Dishes = Dishes.Where(d => d.LangId == language.ID).ToList();
            }
            Dishes = Dishes.OrderBy(d => d.DisplayOrder).ToList();
            Guid dishId = new Guid(id);
            DishDetail = Dishes.Where(d => d.Id == dishId).FirstOrDefault();
            ImageList = ImageService.Instance.GetAllImageByAssociatedId(dishId);
        }
    }
}